//
//  AppDelegate.m
//  Cartoon
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "AppDelegate.h"

#import "BootViewController.h"
#import "CartoonClassificationViewController.h"
#import "CartoonCollectionViewController.h"
#import "CartoonRecommendViewController.h"
#import "CartoonSetViewController.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialSinaHandler.h"
#import "UMSocialWechatHandler.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

@interface AppDelegate ()
@property (nonatomic, strong) UITabBarController *tabBarController;
@property (nonatomic, strong) Reachability *reach;
@property (nonatomic, assign) BOOL full;
@end

@implementation AppDelegate
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    
    [UMSocialData setAppKey:@"563c0ed2e0f55abb80002eab"];
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://ac.qq.com"];
    
    [UMSocialQQHandler setQQWithAppId:@"1104874893" appKey:@"5yzaeLSKYSne6DcX" url:@"http://ac.qq.com"];
    
    [UMSocialWechatHandler setWXAppId:@"wxfe68626d848c8df9" appSecret:@"d4624c36b6795d1d99dcf0547af5443d" url:@"http://ac.qq.com"];
    
    
    // Override point for customization after application launch.
    CartoonClassificationViewController *cartoonClassificationVC = [[CartoonClassificationViewController alloc] init];
   // cartoonClassificationVC.title = @"动漫分类";
    cartoonClassificationVC.title = @"动漫分类";
    UINavigationController *cartoonClassificationNavi = [[UINavigationController alloc] initWithRootViewController:cartoonClassificationVC];
    cartoonClassificationNavi.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:[UIImage imageNamed:@"2@2x.png"] tag:102];
    
    CartoonRecommendViewController *cartoonRecommendVC = [[CartoonRecommendViewController alloc] init];
    cartoonRecommendVC.title = @"动漫推荐";
    UINavigationController *cartoonRecommendNavi = [[UINavigationController alloc] initWithRootViewController:cartoonRecommendVC];
    cartoonRecommendNavi.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"1@2x.png"] tag:101];
    CartoonCollectionViewController *cartoonCollectionVC = [[CartoonCollectionViewController alloc] init];
      cartoonCollectionVC.title = @"我的动漫";
    UINavigationController *cartoonCollectionNavi = [[UINavigationController alloc] initWithRootViewController:cartoonCollectionVC];
    cartoonCollectionNavi.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"3@2x.png"] tag:103];    CartoonSetViewController *cartoonSetVC = [[CartoonSetViewController alloc] init];
      cartoonSetVC.title = @"设置";
    UINavigationController *cartoonSetNavi = [[UINavigationController alloc] initWithRootViewController:cartoonSetVC];
    cartoonSetNavi.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"4@2x.png"] tag:104];
    self.tabBarController = [[UITabBarController alloc] init];
    _tabBarController.viewControllers = @[cartoonRecommendNavi,cartoonClassificationNavi,cartoonCollectionNavi,cartoonSetNavi];
    
    [[UITabBar appearance] setTintColor:[UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 / 255.0 alpha:1]];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRootViewController:) name:@"换根视图控制器" object:nil];
    
    NSUserDefaults *userDe = [NSUserDefaults standardUserDefaults];
    
    NSString *result = [userDe objectForKey:@"firstTime"];
    
    if (result == nil) {
        BootViewController *bootVC = [[BootViewController alloc] init];
        [userDe setObject:@"随便" forKey:@"firstTime"];
        self.window.rootViewController = bootVC;
        
    }else{
        self.window.rootViewController = _tabBarController;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeWindowTransform:) name:@"changeWindowTransform" object:nil];
    
    [self networkStateChange];
    // 监听网络状态改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
    
    // 创建Reachability
    self.reach = [Reachability reachabilityForInternetConnection];
    // 开始监控网络(一旦网络状态发生改变, 就会发出通知kReachabilityChangedNotification)
    [self.reach startNotifier];

    
    
    return YES;
}
// 处理网络状态改变
- (void)networkStateChange
{
    // 1.检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    
    // 2.检测手机是否能上网络(WIFI\3G\2.5G)
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    // 3.判断网络状态
    if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
        NSLog(@"有wifi");
    } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"当前使用手机自带网络进行上网" delegate:self cancelButtonTitle:nil otherButtonTitles:@"继续", nil];
        [alertView show];
        
        NSLog(@"使用手机自带网络进行上网");
    } else { // 没有网络
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"当前没有网络" delegate:self cancelButtonTitle:nil otherButtonTitles:@"继续", nil];
        [alertView show];
        
        NSLog(@"没有网络");
    }
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (!_full) {
        return UIInterfaceOrientationMaskPortrait;
    }else{
        return UIInterfaceOrientationMaskAll;
    }
}
- (void)changeWindowTransform:(NSNotification *)noti
{
    if ([noti.object isEqualToString:@"YES"]) {
        _full = YES;
        NSLog(@"yes");
    }else{
        _full = NO;
    }
}
- (void)changeRootViewController:(NSNotification *)noti
{
    self.window.rootViewController = _tabBarController;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
